<?php
/**
 * 站点管理
 */
namespace logic\site;
use \logic\Base;
class SiteManage extends Base{
    
    
    
    /**
     * 读取一个信息
     */
    public function find($param){
        return D('Site','site')->find($param);
    }
        
    
    
    /**
     * 编辑站点内容
     */
    public function adminEdit($siteid,$param){
        $check = [                    
            'domain'        => '',
            'status'        => ['reg'=>[1,2]],
            'logo'          => '',
            'name'          => ['reg'=>'must','msg'=>'站点名称不能填空','filter'=>1],
            'contactname'   => ['reg'=>'must','msg'=>'联系人不能填空','filter'=> 1],
            'contacttel'    => ['reg'=>'must','msg'=>'联系方式不能填空','filter'=>1],
            'provinceid'    => ['reg'=> 'int'],
            'cityid'        => ['reg'=> 'int'],
            'areaid'        => ['reg'=> 'int'],
            'streetid'      => ['reg'=> 'int'],
            'address'       => '',
            'addressdetail' => ['filter'=>1],
        ];         
        $save = pVerify($check,$param);        
        $Area = new  \logic\sys\Area();
        $save['address'] = $Area->getFullAddress($save['provinceid'],$save['cityid'],$save['areaid'],$save['streetid']);        
        $save = pFilter()->replace('site',$save,$check);
        pFilter()->save('site',$siteid);
        $result = D('Site','site')->edit($siteid,$save);
        return $result;
    }
    
    /**
     * 编辑站点域名
     */
    public function editDomain($siteid,$domain){
        return D('Site','site')->edit($siteid,['domain'=>$domain]);
    }
    
    /**
     * 更新状态
     */
    public function editStatus($siteid){
        $info = D('Site','site')->find(['siteid'=>$siteid]);
        $data['status'] = $info['status']==1?2:1;
        return D('Site','site')->edit($siteid,$data);
    }
    
    
    /**
     * 站点域名规则
     * @param $domaintype 1 独立域名 2 个性域名 3平台三级域名随机
     */
    public static function createDomain($domain,$domaintype){
        return $domain;
        $domain = strtolower($domain);
        if( $domaintype == 1 ){
            return trim($domain,'/');
        }
        if( $domaintype == 2 ){
            if(strlen($domain) < 5 ) logicE('域名已经存在,请重新填写');
            $reg = '/^(site|vip|s)[0-9]*$/';
            $isKeep = preg_match($reg,$domain );
            if( $isKeep )   logicE('域名已经存在,请重新填写1');
            
            $Config = new \logic\sys\config\Config();
            $keep = $Config->getInfo('keep_domain');
            $keepList = implode(',',$keep);
            if( in_array($domain,$keepList)) logicE('域名已经存在,请重新填写2');
            
            $isHas = D('Site','site')->find(['domain'=>$domain]);
            if($isHas) logicE('域名已经存在,请重新填写3');
            return $domain;
        }        
        return '';
    }
}